package com.lulalei.dbviewer.commons.config;

import com.lulalei.dbviewer.db.DBService;
import com.lulalei.dbviewer.db.mysql.dao.MySqlDao;
import com.lulalei.dbviewer.db.mysql.service.MySQLService;
import com.lulalei.dbviewer.strategy.DBStrategyManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration//开启配置
@ConditionalOnClass({DBStrategyManager.class})
public class DBBasicAutoConfiguration {
    @Autowired
    private List<DBService> dbServices;

    @Bean
    @ConditionalOnMissingBean
    public DBStrategyManager dbStrategyManager(){
        return new DBStrategyManager(dbServices);
    }

    @Configuration
    @ConditionalOnClass({MySQLService.class})
    public static class DBServiceAutoConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public MySQLService mySQLService() {
            return new MySQLService();
        }
    }

    @Configuration
    @ConditionalOnClass({MySqlDao.class})
    public static class DBDaoAutoConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public MySqlDao mySqlDao() {
            return new MySqlDao();
        }
    }
}
